package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.ui.fragment.AllOrderFragment;
import com.mengchengnet.chaperonage.ui.fragment.CommentOrderFragment;
import com.mengchengnet.chaperonage.ui.fragment.PayOrderFragment;
import com.mengchengnet.chaperonage.ui.fragment.ServiceOrderFragment;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class MyOrderActivity extends BaseActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private TabLayout.Tab three;
    private TabLayout.Tab four;
    private String mark=null;
    private static final String TAG = "MyOrderActivity";
    private RequestQueue requestQueue;
    private String userId;
    private String userType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        initView();
        //getOrder();
    }

    private void initView() {
        requestQueue= Volley.newRequestQueue(this);
        userId= PreferenceUtil.getString(this,"userId");
         mark=getIntent().getStringExtra("kkey");
        Log.e("yyyyyyyyyyy", "订单参数："+mark );
        toolbarState = TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("我的订单");
        mViewPager= (ViewPager) findViewById(R.id.viewPager);
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(myFragmentPagerAdapter);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.setupWithViewPager(mViewPager);
        userType=PreferenceUtil.getString(this,"userType");
        Log.e(TAG, "用户类型: "+userType);

    }


    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private String[] mTitles = new String[]{"全部订单", "待付款", "待护理", "待评价"};

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 3) {
                return new CommentOrderFragment();
            }else if(position == 2){
                return new ServiceOrderFragment();
            }
            else if(position == 1){
                return new PayOrderFragment();
            }
            return new AllOrderFragment();
        }

        @Override
        public int getCount() {
            return mTitles.length;
        }

        //ViewPager与TabLayout绑定后，这里获取到PageTitle就是Tab的Text
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mark.equals("0")){
            mViewPager.setCurrentItem(1);
            two = mTabLayout.getTabAt(3);
        }else if(mark.equals("1")){
            mViewPager.setCurrentItem(2);
            three=mTabLayout.getTabAt(1);
        }
        else if(mark.equals("2")){
            mViewPager.setCurrentItem(3);
            four=mTabLayout.getTabAt(2);
        }else if(mark.equals("3")){
            mViewPager.setCurrentItem(0);
            one = mTabLayout.getTabAt(0);
        }


        }

    private void getOrder() {
        StringRequest request = new StringRequest(Request.Method.POST, Url.GetOrderList,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("订单界面", "返回值："+result);
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }

                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){


                            }
                            else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("loginId","170");
                map.put("status","0");

                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }

}